package com.ztesoft.sca.controller.user;

import com.google.gson.Gson;
import com.ztesoft.sca.common.Constants;
import com.ztesoft.sca.model.user.ImPrivilegePo;
import com.ztesoft.sca.model.user.ImRolePo;
import com.ztesoft.sca.service.user.ImRoleService;
import com.ztesoft.sca.util.exception.ExceptionUtil;
import com.ztesoft.sca.util.json.GsonUtils;
import com.ztesoft.sca.util.message.ResponseUtils;
import com.ztesoft.sca.util.seq.SequenceCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liang.aozhao
 * @created 2018/5/17 16:47
 */
@RestController
@RequestMapping(value = "/role")
public class ImRoleController {

    private static final Logger logger = LoggerFactory.getLogger(ImRoleController.class);
    @Autowired
    ImRoleService imRoleService;

    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public String addImRole(@RequestBody ImRolePo imRolePo) {
        if (logger.isDebugEnabled()) {
            logger.debug(imRolePo.toString());
        }
        long roleId;
        try {
            roleId = SequenceCreator.getSequence("IM_ROLE_SEQ");
            imRolePo.setRoleId(roleId);
            imRoleService.addImRole(imRolePo);
        } catch (Exception e) {
            logger.error("新增角色失败！", e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        logger.info("新增角色,roleid 为" + roleId);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, roleId);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String deleteImRole(@RequestBody ImRolePo imRolePo) {
        if (logger.isDebugEnabled()) {
            logger.debug(imRolePo.toString());
        }
        try {
            //可根据roleId 或者 roleCode删除
            imRoleService.deleteImRole(imRolePo);
        } catch (Exception e) {
            logger.error("删除角色失败", e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public String updateImRole(@RequestBody ImRolePo imRolePo) {
        if (logger.isDebugEnabled()) {
            logger.debug(imRolePo.toString());
        }
        try {
            if (imRolePo.getRoleId() != null) {
                imRoleService.updateImRole(imRolePo);
            } else {
                return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, "缺失roleId");
            }
        } catch (Exception e) {
            logger.error("更新角色失败", e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    @RequestMapping(value = "/getImRole", method = RequestMethod.POST)
    public String getImRole(@RequestBody ImRolePo imRolePo) {
        if (logger.isDebugEnabled()) {
            logger.debug(imRolePo.toString());
        }
        ImRolePo imRole = new ImRolePo();
        try {
            imRole = imRoleService.getImRole(imRolePo);
        } catch (Exception e) {
            logger.error("查询角色失败", e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imRole);
    }

    @RequestMapping(value = "/list/all", method = RequestMethod.GET)
    public String listAllImRoles() {
        List<ImRolePo> imRolePoList;
        try {
            imRolePoList = imRoleService.listAllImRoles();
        } catch (Exception e) {
            logger.error("查询角色失败", e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imRolePoList);
    }

    @RequestMapping(value = "/listByUserId", method = RequestMethod.GET)
    public String listImRoleByUserId(@RequestParam(value = "userid") Long userId) {
        List<ImRolePo> imRolePoList;
        try {
            if(userId != null){
                imRolePoList = imRoleService.listImRoleByUserId(userId);
            }else {
                return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, "缺少参数userid");
            }
        } catch (Exception e) {
            logger.error("根据用户id查询角色失败",e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imRolePoList);
    }


    @RequestMapping(value = "/listByOrgId",method = RequestMethod.GET)
     public String listImRoleByOrgId(@RequestParam(value = "orgid") Long orgId) {
        List<ImRolePo> imRolePoList;
        try {
            if(orgId != null){
                imRolePoList = imRoleService.listImRoleByOrgId(orgId);
            }else {
                return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, "缺少参数orgid");
            }
        } catch (Exception e) {
            logger.error("根据组织id查询角色失败",e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imRolePoList);
    }

    @RequestMapping(value = "/addRela",method = RequestMethod.POST)
    public String addImRoleOrg(@RequestBody String reqParams) {
        if(logger.isDebugEnabled()){
            logger.debug(reqParams);
        }
        try {
            Long roleId = (Long) GsonUtils.toMap(reqParams).get("roleId");
            Long orgId = (Long) GsonUtils.toMap(reqParams).get("orgId");
            if(roleId != null && orgId != null){
                imRoleService.addImRoleOrg(roleId,orgId);
            }else {
                return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, "缺失参数roleId和orgId");
            }
        }catch (Exception e){
            logger.error("新增角色-组织关系失败",e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    @RequestMapping(value = "/deleteRela",method = RequestMethod.POST)
    public String deleteImRoleOrg(@RequestBody String reqParams) {
        if(logger.isDebugEnabled()){
            logger.debug(reqParams);
        }
        try {
            Long roleId = (Long) GsonUtils.toMap(reqParams).get("roleId");
            Long orgId = (Long) GsonUtils.toMap(reqParams).get("orgId");
            if(roleId != null && orgId != null){
                imRoleService.deleteImRoleOrg(roleId,orgId);
            }else {
                return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, "缺失参数roleId和orgId");
            }
        }catch (Exception e){
            logger.error("删除角色-组织关系失败",e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    @RequestMapping(value = "/listPrivilege",method = RequestMethod.GET)
    public String listImPrivilegeByRoleId(@RequestParam(value = "roleid") Long roleId) {
        List<ImPrivilegePo> imPrivilegePoList;
        try {
            if(roleId != null){
                imPrivilegePoList = imRoleService.listImPrivilegeByRoleId(roleId);
            }else {
                return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR,"缺少参数roleid");
            }
        } catch (Exception e) {
            logger.error("查询角色权限失败",e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imPrivilegePoList);
    }
}
